package com.cy.store.service;

import com.cy.store.entity.User;
import com.cy.store.mapper.UserMapper;
import com.cy.store.service.ex.ServiceException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;

@SpringBootTest//表示这是一个springboot测试类，不会随着应用打包
@RunWith(SpringRunner.class)//启动这个单元测试类
public class UserServicetest {
    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired
    private IUserService userService;

    /**
     * 测试注册
     */
    @Test
    public void reg() {
        try {
            User user = new User();
            user.setUsername("xiaoke1");
            user.setPassword("123456");
            userService.reg(user);
            System.out.println("OK");
        } catch (ServiceException e) {
            System.out.println("打印异常信息：");
            System.out.println(e.getMessage());
            System.out.println("打印异常类的对象的名称：");
            System.out.println(e.getClass().getSimpleName());
        }
    }

    /**
     * 完整注册方法测试
     */
    @Test
    public void testInsert() {
        User user = new User();
        user.setUsername("123");
        user.setPassword("123");

        userService.reg(user);
    }

    /**
     * 登录功能测试
     */
    @Test
    public void testLogin() {
        String username = "123";
        String password = "123";

        User login = userService.login(username, password);
        System.out.println(login);
    }

    @Test
    public void testupDate() {

        userService.changePassword(8, "123", "123", "123");
    }

    @Test
    public void testfindByID() {
        User byUid = userService.getByUid(1);
        System.out.println(byUid);
    }

    @Test
    public void testupdateByid() {
        User byUid = userService.getByUid(8);
        userService.changeInfo(8, "123", byUid);
    }

    /**
     * 更新头像
     */
    @Test
    public void testdataAtatar() {
        userService.changeAvatar(1, "/upload/avatar.jpg", "管理员");
    }

}
